package com.visualexpress.dao;

import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Transaction;
import org.hibernate.criterion.Example;

import com.visualexpress.dao.BaseHibernateDAO;
import com.visualexpress.domain.Location;
import com.visualexpress.domain.LocationAvertime;

/**
 * A data access object (DAO) providing persistence and search support for
 * LocationAvertime entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see com.visualexpress.domain.LocationAvertime
 * @author MyEclipse Persistence Tools
 */

public class LocationAvertimeDAO extends BaseHibernateDAO {
	private static final Log log = LogFactory.getLog(LocationAvertimeDAO.class);
	// property constants
	
	public static final String LOCATION="location";
	public static final String TIME_STAY = "timeStay";
	public static final String AMOUNT = "amount";
	public static final String WEIGHT="weight";
	public static final String RECENT_TIME="recentTime";

	public void save(LocationAvertime transientInstance) {
		log.debug("saving LocationAvertime instance");
		try {
			Transaction t = this.getSession().beginTransaction();
			t.begin();
			getSession().save(transientInstance);
			t.commit();
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}
	public void saveOrUpdate(LocationAvertime transientInstance) {
		log.debug("saving LocationAvertime instance");
		try {
			Transaction t = this.getSession().beginTransaction();
			t.begin();
			getSession().saveOrUpdate(transientInstance);
			t.commit();
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(LocationAvertime persistentInstance) {
		log.debug("deleting LocationAvertime instance");
		try {
			Transaction t = this.getSession().beginTransaction();
			t.begin();
			getSession().delete(persistentInstance);
			t.commit();
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public LocationAvertime findById(java.lang.String id) {
		log.debug("getting LocationAvertime instance with id: " + id);
		try {
			LocationAvertime instance = (LocationAvertime) getSession().get(
					"com.visualexpress.domain.LocationAvertime", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(LocationAvertime instance) {
		log.debug("finding LocationAvertime instance by example");
		try {
			List results = getSession().createCriteria(
					"com.visualexpress.domain.LocationAvertime").add(
					Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding LocationAvertime instance with property: "
				+ propertyName + ", value: " + value);
		try {
			String queryString = "from LocationAvertime as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}
	
	public List getAvgTimeStay() {
		log.debug("getting the average of timeStay for LocationAvertime");
		try {
			String queryString = "select avg(locAverTime."
				+ TIME_STAY + ") from LocationAvertime locAverTime";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}
	
	public List findByLocation(Object location){
		return findByProperty(LOCATION,location);
	}

	public List findByTimeStay(Object timeStay) {
		return findByProperty(TIME_STAY, timeStay);
	}

	public List findAll() {
		log.debug("finding all LocationAvertime instances");
		try {
			String queryString = "from LocationAvertime";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public LocationAvertime merge(LocationAvertime detachedInstance) {
		log.debug("merging LocationAvertime instance");
		try {
			LocationAvertime result = (LocationAvertime) getSession().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(LocationAvertime instance) {
		log.debug("attaching dirty LocationAvertime instance");
		try {
			Transaction t = this.getSession().beginTransaction();
			t.begin();
			getSession().saveOrUpdate(instance);
			t.commit();
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(LocationAvertime instance) {
		log.debug("attaching clean LocationAvertime instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
}